package cn.zclong.user.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.jasper.tagplugins.jstl.core.Param;

import cn.itcast.jdbc.TxQueryRunner;
import cn.zclong.user.domain.User;

/*
 * User持久层，与数据库交互
 */
public class UserDao {
	private QueryRunner qr = new TxQueryRunner();

	/*
	 * 根据用户名查询
	 */
	public User findByUsername(String username) {
		try {
			String sql = "select * from t_user where username=?";
			return qr.query(sql, new BeanHandler<User>(User.class), username);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/*
	 * 根据邮编查询
	 */
	public User findByEmail(String email) {
		try {
			String sql = "select * from t_user where email=?";
			return qr.query(sql, new BeanHandler<User>(User.class), email);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	/*
	 * 根据激活码查询
	 */
	public User findByCode(String code) {
		try {
			String sql = "select * from t_user where code=?";
			return qr.query(sql, new BeanHandler<User>(User.class), code);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	/*
	 * 修改激活状态
	 */
	public void updateState(String uid, boolean state) {
		try {
			String sql = "update t_user set state=? where uid=?";
			qr.update(sql, state, uid);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	/*
	 * 修改积分
	 */
	public void updateIntegral(String uid, int integral) {
		try {
			String sql = "update t_user set integral=? where uid=?";
			qr.update(sql, integral, uid);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/*
	 * 添加用户
	 */
	public void addUser(User user) {
		try{
			String sql = "insert into t_user values(?,?,?,?,?,?,?,?,?)";
			Object[] param = {user.getUid(), user.getUsername(), user.getPassword(), user.getEmail(), user.getCode(), user.isState(), user.getIp(), user.getIntegral(), user.getType() };
			qr.update(sql, param);
		}catch(SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/*
	 * 管理员查询所有的用户信息
	 */
	public List<User> findAll() {
		try{
			String sql = "select * from t_user";
			return qr.query(sql, new BeanListHandler<User>(User.class));
		}catch(SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/*
	 * 管理员删除用户
	 */
	public void delete(String uid) {
		try{
			String sql = "delete from t_user where uid=?";
			qr.update(sql, uid);
		}catch(SQLException e) {
			throw new RuntimeException(e);
		}		
	}

	/*
	 * 通过uid获取用户的激活状态
	 */
	public boolean findByUid(String uid) {
		try{
			String sql = "select state from t_user where uid=?";
			return (boolean) qr.query(sql, new ScalarHandler(), uid);
		}catch(SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 管理员修改用户之前的加载工作
	 */
	public User load(String uid) {
		try{
			String sql = "select * from t_user where uid=?";
			return qr.query(sql, new BeanHandler<User>(User.class), uid);
		}catch(SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 管理员修改用户
	 */
	public void edit(User user) {
		try {
			String sql = "update t_user set username=?,password=? where uid=?";
			qr.update(sql, user.getUsername(), user.getPassword(), user.getUid());
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	/*
	 * 管理员登录
	 */
	public User adminLogin(String username) {
		try {
			String sql = "select * from t_user where username=? and type=?";
			return qr.query(sql, new BeanHandler<User>(User.class), username, 0);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}
